/*!
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * 
 * version #version, #date by Fuchun
 * Depends on jQuery 1.3.x
 * Depends on giant.core 2.0.x
 * Depends on ui.component 2.0.x
 */

/**
 * @class 关于好友的常用方法的工具类。
 */
var GFriends = window.GFriends = function() {};

(function($) {

window.__rfDialog = null;
var _completeCallBack = null;

/**
 * 弹出好友请求的浮层。
 * 
 * @param {HTMLElement} o 添加好友的按钮或者链接对象。
 * @param {String} fuid 经加密的要请求的用户的Id。
 * @returns {String} 
 */
GFriends.popupFriendRequest = function(o, fuid, isCrossdomain,compeleteCallBack) {
    if(!giant.isBool(isCrossdomain))
        isCrossdomain = false;
    _completeCallBack = compeleteCallBack||null;
    GiantAjax.responseJson({
        crossdomain: isCrossdomain,
        url: '/friend/ajaxPreAddFriend.do?rn=' + Math.random(),
        params: { fuid: fuid },
        success: function(jdata) {
            if(jdata.status == giant.Status.error) {
                alert(jdata.message);
                return;
            }
            var shtml = GFriends._getRequestPageHtml(jdata, fuid, isCrossdomain,compeleteCallBack);
            window.__rfDialog = $.dialog({
                title: '请求加为好友',
                isChange: true,
                isAjax: false,
                content: shtml,
                index: fuid + '_mk',
                width: 400
            });
        }
    });
};

// 关闭好友请求界面
GFriends.closeFriendRequest = function() {
    window.__rfDialog.close();
    window.__rfDialog.remove();
};

GFriends._getRequestPageHtml = function(jdata, fuid, isCrossdomain,compeleteCallBack) {
    var buf = new StringBuilder();
    var friend = {};
    buf.append('<form id="form_request_friend" name="formRequestFriend" method="post" ');
    buf.append('action="').append(Path['domain']).append('/friend/ajaxAddFriend.do">');
    buf.append('<div class="f-dig-ctn">');
    if(jdata.status != giant.Status.error) {
        friend = jdata.values;
        buf.append('<input type="hidden" name="friend.fUid" value="').append(fuid).append('" />');
        buf.append('<input type="hidden" name="friend.friendUserName" value="').append(friend.userName).append('" />');
        buf.append('  <div class="head-big fleft mr10">\n');
        buf.append('  <a title="').append(friend.userName).append('" ');
        buf.append('class="img" href="javascript:void(0);" onclick="return false;">');
        buf.append('<img src="').append(Path['userfiles']).append(friend.headpic).append('" onerror="ImageLoad(this, ImageLoad.HEADPIC_BIG, \'');
        buf.append(friend.gender).append('\');" /></a>\n').append('  </div>\n');
        buf.append('  <div id="friend_info" class="fleft w240 inline">\n');
        if(friend.access == 1) {
            if(friend.askFlag=="false") {
                buf.append('    <p class="llight1">对方通过验证后，你就能和').append(friend.genderName).append('成为好友。');
                buf.append('    <br/>给').append(friend.genderName).append('的验证信息:</p>');
            } else {
                buf.append('    <p class="llight1">').append(friend.userName);
                buf.append('说：').append(friend.askTip).append('<br/>请输入验证信息:</p>');
            }
            buf.append('<textarea name="friend.description" id="txt_friendreq_info" class="w240" onfocus="$(this).select();" ');
            buf.append('onkeyup="GFriends.writeRequestInfo(this, \'#writeable_num\');">')
            .append('嗨！我想和你交个朋友哦^-^快点同意加我为好友吧~嘿嘿！').append('</textarea>');
            buf.append('<p class="tright llight1">*还可以输入<span id="writeable_num">32</span>个字</p>');
        } else if(friend.access == 0){
            buf.append('<p class="llight1 mb20">非常遗憾！</p>');
            buf.append('<p class="llight1">根据').append(friend.userName).append('的设置，只有');
            buf.append(friend.genderName).append('的好友的好友才可以给').append(friend.genderName);
            buf.append('发送好友请求！</p>');
        } else {
            buf.append(jdata.message);
        }
        buf.append('  </div>  <div class="clear"></div>');
    }
    buf.append('</div>\n<div class="ui-popup-ext">');
    if(friend.access == 1) {
        buf.append('<input id="sendbtn" type="button" value="发 送" class="btn-small btn-01" ');
        buf.append('onclick="GFriends.sendRequest(this, \'#form_request_friend\', \'').append(fuid).append('\', ').append(isCrossdomain.toString()).append(');"/>');
        buf.append('<input id="closebtn" type="button" value="取 消" class="btn-small btn-02" ');
        buf.append('onclick="GFriends.closeFriendRequest();"/>');
    } else {
        buf.append('<input id="closebtn" type="button" value="关 闭" class="btn-small btn-02" ');
        buf.append('onclick="GFriends.closeFriendRequest();"/>');
    }
    buf.append('</div></form>\n');
    return buf.toString();
};

/**
 * 发送好友请求信息。
 * 
 * @param {HTMLInputElement} o 发送控钮对象
 * @param {String} formId 表单Id。
 * @param {Boolean} isCrossdomain 是否进行跨域请求。
 */
GFriends.sendRequest = function(o, formId, fuid, isCrossdomain){
    $(o).removeClass('btn-01').addClass('btn-02').attr('disabled', true);
    if(!giant.isBool(isCrossdomain))
        isCrossdomain = false;
    var sendSuccess = function(jdata) {
        GFriends.closeFriendRequest();
        $.msgbox({
            message: jdata.message,
            confirm: {
                    click: function(){
                        if(giant.isFunction(_completeCallBack)){
                        	_completeCallBack(fuid);
                        }
                    }
                }
        });
        $(o).removeClass('btn-02').addClass('btn-01').attr('disabled', false);
        window.__rfDialog.remove();
    };
    var beforeSubmit = function() {
        if($('#txt_friendreq_info').val().trim() == "") {
            $.msgbox({
                message: '请输入好友请求信息!',
                confirm: {
                    click: function() {
                        $(o).removeClass('btn-02').addClass('btn-01').attr('disabled', false);
                    }
                },
                gloomy: false
            });
            return false;
        }
        return true;
    };
    if(isCrossdomain) {
        GiantAjax.responseJson({
            before: beforeSubmit,
            url: $(formId).attr('action') + '?' + $(formId).formSerialize(),
            crossdomain: isCrossdomain,
            success: sendSuccess
        });
    } else {
        GiantAjax.formSubmit(formId, {
            beforeSubmit: beforeSubmit,
            success: sendSuccess
        });
    }
};

GFriends.writeRequestInfo = function(o, nid) {
    var textarea = $(o), value = textarea.val().trim();
    if(value.length >= 60) {
        textarea.val(value.substring(0,59));
    };
    var ableNum=60 - value.length;
    $(nid).text(ableNum<0?0:ableNum);
};
})(jQuery);